package com.test.base.api;

import com.test.base.dao.*;
import com.test.base.entity.*;
import com.test.base.utils.Assist;
import com.test.base.vo.api.CategoryListVo;
import com.test.base.vo.api.IndexApiVo;
import com.test.base.vo.common.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "api/index")
@Api(description = "首页数据")
public class IndexApi {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private NideshopAdDao nideshopAdDao;
    @Autowired
    private NideshopChannelDao nideshopChannelDao;
    @Autowired
    private NideshopGoodsDao nideshopGoodsDao;
    @Autowired
    private NideshopBrandDao nideshopBrandDao;
    @Autowired
    private NideshopTopicDao nideshopTopicDao;
    @Autowired
    private NideshopCategoryDao nideshopCategoryDao;


    @ApiOperation(value = "获取首页数据", notes = "")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ResponseBody
    public ResultData<IndexApiVo> getUserList() {
        IndexApiVo indexApiVo = new IndexApiVo();
        indexApiVo.setBanner(getAd());
        indexApiVo.setChannel(getChannel());
        indexApiVo.setNewGoods(getnewGoods());
        indexApiVo.setHotGoods(getHotGoods());
        indexApiVo.setBrandList(getBrand());
        indexApiVo.setTopicList(getTopic());
        indexApiVo.setCategoryList(getCategory());
        return new ResultData<IndexApiVo>(indexApiVo,null,true);
    }

    /**
     * 首页轮播图
     *
     * @return
     */
    private List<NideshopAd> getAd() {
        Assist ad = new Assist();
        ad.setRequires(ad.andEq("ad_position_id", 1));
        return nideshopAdDao.selectNideshopAd(ad);
    }

    /**
     * 待补充
     *
     * @return
     */
    private List<NideshopChannel> getChannel() {
        Assist channel = new Assist();
        channel.setOrder("sort_order", true);
        return nideshopChannelDao.selectNideshopChannel(channel);
    }

    /**
     * 新商品
     *
     * @return
     */
    private List<NideshopGoods> getnewGoods() {
        Assist newGood = new Assist();
        newGood.setRequires(newGood.andEq("is_new", 1));
        newGood.setStartRow(0);
        newGood.setRowSize(4);
        newGood.setResultColumn("id,name,list_pic_url,retail_price");
        return nideshopGoodsDao.selectNideshopGoods(newGood);
    }

    /**
     * 热销商品
     *
     * @return
     */
    private List<NideshopGoods> getHotGoods() {
        Assist hotGood = new Assist();
        hotGood.setRequires(hotGood.andEq("is_hot", 1));
        hotGood.setStartRow(0);
        hotGood.setRowSize(3);
        hotGood.setResultColumn("id,name,list_pic_url,retail_price");
        return nideshopGoodsDao.selectNideshopGoods(hotGood);
    }

    /**
     * 待补充
     *
     * @return
     */
    private List<NideshopBrand> getBrand() {
        Assist brand = new Assist();
        brand.setRequires(brand.andEq("is_new", 1));
        brand.setStartRow(0);
        brand.setRowSize(4);
        brand.setOrder("sort_order", true);
        return nideshopBrandDao.selectNideshopBrand(brand);
    }

    /**
     * 待补充
     *
     * @return
     */
    private List<NideshopTopic> getTopic() {
        Assist topic = new Assist();
        topic.setStartRow(0);
        topic.setRowSize(3);
        return nideshopTopicDao.selectNideshopTopic(topic);
    }

    private List<CategoryListVo> getCategory() {
        List<CategoryListVo> result = new ArrayList<>();
        Assist category = new Assist();
        category.setRequires(category.andEq("parent_id", 0));
        category.setRequires(category.andNeq("name", "推荐"));
        List<NideshopCategory> categories = nideshopCategoryDao.selectNideshopCategory(category);
        for (NideshopCategory ci : categories) {
            Assist categoryGood = new Assist();
            categoryGood.setRequires(categoryGood.customRequire("category_id in (select id from nideshop_category where parent_id = ", ci.getId(), ")"));
            categoryGood.setStartRow(0);
            categoryGood.setRowSize(7);
            categoryGood.setResultColumn("id,name,list_pic_url,retail_price");
            List<NideshopGoods> categoryGoods = nideshopGoodsDao.selectNideshopGoods(categoryGood);
            CategoryListVo clv = new CategoryListVo();
            clv.setId(ci.getId());
            clv.setName(ci.getName());
            clv.setGoodsList(categoryGoods);
            result.add(clv);
        }
        return result;
    }
}
